开发者

SectionedAdapter (inflate layout)

I have a LinearLayout which I wanna inflate into my SectionedAdapter as a Header. LinearLayout have one ImageView and one TextView. I tried this but it's not working...

SectionedAdapter adapter = new SectionedAdapter() {
 protected View getHeaderView(String caption, int index, View convertView, ViewGroup parent) {
 TextView result=(TextView)convertView;

if (convertView == null) {
result = (TextView) getLayoutInflater().inflate(R.layout.my_main_layout, null).findViewById(R.id.label);
}
result.setText(caption);

return(result);
 }
};

I get

01-09 21:23:02.336: ERROR/AndroidRuntime(308): FATAL EXCEPTION: main
01-09 21:23:02.336: ERROR/AndroidRuntime(308): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.ListView.setupChild(ListView.java:1761)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.ListView.makeAndAddView(ListView.java:1730)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.ListView.fillDown(ListView.java:652)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.ListView.fillFromTop(ListView.java:709)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.ListView.layoutChildren(ListView.java:1580)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.view.View.layout(View.java:7035)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.view.View.layout(View.java:7035)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.view.View.layout(View.java:7035)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.view.View.layout(View.java:7035)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.view.View.layout(View.java:7035)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.view.View.layout(View.java:7035)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-09 21:23:02.336: ERROR/An开发者_JS百科droidRuntime(308):     at android.view.View.layout(View.java:7035)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.os.Looper.loop(Looper.java:123)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at java.lang.reflect.Method.invoke(Method.java:521)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-09 21:23:02.336: ERROR/AndroidRuntime(308):     at dalvik.system.NativeStart.main(Native Method)

Ofcourse, casting (TextView) is a problem..?


THe error means that you are trying to cast something a cast is something that looks like this:

(SomeClass) SomeFunctionThatReturnsAnObject();

If the functions returns an object that is not from the correct class, you will get this error. I don't know what line returned the error, and what the linenumbers in your code are, but you have a "findViewById" call. That will return a View. If that View is a TextView, you can cast it to (TextView), but if it's not, you can't.


This should work: (TextView)((LinearLayout)getLayoutInflater().inflate(R.layout.my_main_layout, null)).findViewById(R.id.label);

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜