开发者

Custom widget throws a NullPointerException on setRequestedViewSize

开发者 https://www.devze.com 2023-02-05 06:23 出处:网络
Good morning folks, I\'m working on an Android app and I\'ve run into a bit of a problem. I have created a new class that extends View.I have overridden the appropriate methods (the Constructor, onDr

Good morning folks,

I'm working on an Android app and I've run into a bit of a problem. I have created a new class that extends View. I have overridden the appropriate methods (the Constructor, onDraw, onMeasure) and am instantiating the View through the applications layout XML (which is called main.xml).

Within the source code of my app I have the following code:

public class CustomViewTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

/* I *think* I need to setContentView before I actually can use any of the widgets on the form. */
        setContentView(R.layout.main);       
        Button b = (Button)findViewById(R.id.button);

        b.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
// my new widget
newWidget s = (newWidget)findViewById(R.id.testWidget);
s.setRequestedViewSize(300);
   }

        });


    }
}

the problem here, is that s.setRequestedViewSize(300) throws a NullPointerException. Has anyone run into this before or can lend me some advice?

[EDIT] The 开发者_如何学Pythonmain.XML looks like this:

It returning null, but the XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <com.testing.CustomViewTest02.newWidget
        android:id="@+id/testWidget"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></Button>
</LinearLayout>

Thanks you! Sam


The problem was in my widget, I had the correct constructor (Context context AttributeSet attr); HOWEVER - super(context) was being called - not super(context, attr). Once I fixed that, life was better.

Thanks for the info everyone!


You have no view with the R.id.testWidget ID: it's not about nonstatic methods. Since findViewById() returns null, the next line throws an exception.


Do you reference your custom view inside your main.xml? @Pontus is right. There is no View being returned from the findViewById() method.

It would probably be better to get your reference to it in the onCreate method (call the constructor there if necessary) and then set it visible or add it to another view (whatever you are trying to do with it) once the button is clicked.

EDIT: Based on your XML above. Does your custom class implement a constructor that takes a Context and an AttributeSet? The constructor with the AttributeSet is the constructor that Android will call to create your class from the XML layout.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号