我如何使用relativelayout作为listview的标题

时间:2016-04-21 20:33:20

标签: android listview android-intent

我有一个简单的应用程序,允许用户发布其他人可以发表评论的热门帖子。当用户点击列表视图上的帖子时,我有两个活动mainActivity和commentActivity.On mainActivity

Intent intent = new Intent(MainActivity.this, CommentActivity.class);
intent.putExtra("Appid", post.getObjectId());
intent.putExtra("Username", post.getUser().getUsername());
intent.putExtra("Text", post.getText());
intent.putExtra("vote",Integer.toString(post.getVote()));
intent.putExtra("Timestamp",Long.toString(post.getTimestamp()));
startActivity(intent);

关于我检索的评论活动

String  username = intent.getStringExtra("Username");
String text     = intent.getStringExtra("Text");
post_id         = intent.getStringExtra("Appid");
String sum      = intent.getStringExtra("vote");
String time     = intent.getStringExtra("Timestamp");

并将其显示在列表视图上方的相对布局上。我想要的是相对布局与listview一起滚动为1。我所做的是为相对布局设置一个单独的xml,称为header .xml,与comment_activity.xml分开。在commentActivity上我试图给header.xml充气并将其用作listview的标题

View view= getLayoutInflater().inflate(R.layout.header,null);    
TextView vote_count = (TextView) view.findViewById(R.id.txtVote_Count);
TextView txtusername =(TextView) view.findViewById(R.id.txtUsername_view_Respond);
TextView Statusmsg =(TextView)  view.findViewById(R.id.txtContent_view_Respond);


txtusername.setText(username);
Statusmsg.setText(text);
timestamp.setText(timeAgo);

ListView listView = (ListView) 
findViewById(R.id.comment_list_view);
listView.addHeaderView(view);
listView.setAdapter(Adapter); 

`

我得到一个java.lang.NullPointerException。 这是我的堆栈跟踪“

  04-20 20:13:29.737  20254-20254/com.example.machimanapc.howzit W/System.err? at android.os.Looper.loop(Looper.java:193)
04-20 20:13:29.737  20254-20254/com.example.machimanapc.howzit W/System.err? at android.app.ActivityThread.main(ActivityThread.java:5299)
04-20 20:13:29.737  20254-20254/com.example.machimanapc.howzit W/System.err? at java.lang.reflect.Method.invokeNative(Native Method)
04-20 20:13:29.737  20254-20254/com.example.machimanapc.howzit W/System.err? at java.lang.reflect.Method.invoke(Method.java:515)
04-20 20:13:29.739  20254-20254/com.example.machimanapc.howzit W/System.err? at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
04-20 20:13:29.740  20254-20254/com.example.machimanapc.howzit W/System.err? at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
04-20 20:13:29.740  20254-20254/com.example.machimanapc.howzit W/System.err? at dalvik.system.NativeStart.main(Native Method)
04-20 20:13:29.740  20254-20254/com.example.machimanapc.howzit W/System.err? Caused by: java.lang.NullPointerException
04-20 20:13:29.746  20254-20254/com.example.machimanapc.howzit W/System.err? at com.example.machimanapc.howzit.CommentActivity.onCreate(CommentActivity.java:196)
04-20 20:13:29.746  20254-20254/com.example.machimanapc.howzit W/System.err? at android.app.Activity.performCreate(Activity.java:5264)
04-20 20:13:29.746  20254-20254/com.example.machimanapc.howzit W/System.err? at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
04-20 20:13:29.746  20254-20254/com.example.machimanapc.howzit W/System.err? at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
04-20 20:13:29.746  20254-20254/com.example.machimanapc.howzit W/System.err? ... 11 more
04-20 20:13:29.746  20254-20254/com.example.machimanapc.howzit W/dalvikvm? threadid=1: calling UncaughtExceptionHandler
04-20 20:13:29.751  20254-20254/com.example.machimanapc.howzit E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.example.machimanapc.howzit, PID: 20254
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.machimanapc.howzit/com.example.machimanapc.howzit.CommentActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.machimanapc.howzit.CommentActivity.onCreate(CommentActivity.java:196)
            at android.app.Activity.performCreate(Activity.java:5264)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)
04-20 20:13:30.053  20254-23455/com.example.machimanapc.howzit D/dalvikvm? create interp thread : stack size=128KB
04-20 20:13:30.053  20254-23455/com.example.machimanapc.howzit D/dalvikvm? create new thread
04-20 20:13:30.053  20254-23455/com.example.machimanapc.howzit D/dalvikvm? new thread created
04-20 20:13:30.053  20254-23455/com.example.machimanapc.howzit D/dalvikvm? update thread list
04-20 20:13:30.055  20254-23457/com.example.machimanapc.howzit D/dalvikvm? threadid=14: interp stack at 0x551f9000
04-20 20:13:30.055  20254-23457/com.example.machimanapc.howzit D/dalvikvm? init ref table
04-20 20:13:30.055  20254-23457/com.example.machimanapc.howzit D/dalvikvm? init mutex
04-20 20:13:30.055  20254-23457/com.example.machimanapc.howzit D/dalvikvm? threadid=14: created from interp
04-20 20:13:30.055  20254-23455/com.example.machimanapc.howzit D/dalvikvm? start new thread
04-20 20:13:30.055  20254-23457/com.example.machimanapc.howzit D/dalvikvm? threadid=14: notify debugger
04-20 20:13:30.055  20254-23457/com.example.machimanapc.howzit D/dalvikvm? threadid=14 (Task.BACKGROUND_EXECUTOR-thread-13): calling run()
04-20 20:13:30.056  20254-23457/com.example.machimanapc.howzit D/dalvikvm? create interp thread : stack size=128KB
04-20 20:13:30.057  20254-23457/com.example.machimanapc.howzit D/dalvikvm? create new thread
04-20 20:13:30.057  20254-23457/com.example.machimanapc.howzit D/dalvikvm? new thread created
04-20 20:13:30.057  20254-23457/com.example.machimanapc.howzit D/dalvikvm? update thread list
04-20 20:13:30.059  20254-23458/com.example.machimanapc.howzit D/dalvikvm? threadid=15: interp stack at 0x55219000
04-20 20:13:30.059  20254-23458/com.example.machimanapc.howzit D/dalvikvm? init ref table
04-20 20:13:30.059  20254-23458/com.example.machimanapc.howzit D/dalvikvm? init mutex
04-20 20:13:30.059  20254-23458/com.example.machimanapc.howzit D/dalvikvm? threadid=15: created from interp
04-20 20:13:30.059  20254-23457/com.example.machimanapc.howzit D/dalvikvm? start new thread
04-20 20:13:30.059  20254-23458/com.example.machimanapc.howzit D/dalvikvm? threadid=15: notify debugger
04-20 20:13:30.059  20254-23458/com.example.machimanapc.howzit D/dalvikvm? threadid=15 (Task.BACKGROUND_EXECUTOR-thread-14): calling run()
04-20 20:13:30.060  20254-20272/com.example.machimanapc.howzit D/dalvikvm? create interp thread : stack size=128KB
04-20 20:13:30.060  20254-20272/com.example.machimanapc.howzit D/dalvikvm? create new thread
04-20 20:13:30.060  20254-20272/com.example.machimanapc.howzit D/dalvikvm? new thread created
04-20 20:13:30.060  20254-20272/com.example.machimanapc.howzit D/dalvikvm? update thread list
04-20 20:13:30.061  20254-23460/com.example.machimanapc.howzit D/dalvikvm? threadid=20: interp stack at 0x55239000
04-20 20:13:30.061  20254-23460/com.example.machimanapc.howzit D/dalvikvm? init ref table
04-20 20:13:30.061  20254-23460/com.example.machimanapc.howzit D/dalvikvm? init mutex
04-20 20:13:30.061  20254-23460/com.example.machimanapc.howzit D/dalvikvm? threadid=20: created from interp
04-20 20:13:30.061  20254-20272/com.example.machimanapc.howzit D/dalvikvm? start new thread
04-20 20:13:30.061  20254-23460/com.example.machimanapc.howzit D/dalvikvm? threadid=20: notify debugger
04-20 20:13:30.061  20254-23460/com.example.machimanapc.howzit D/dalvikvm? threadid=20 (Task.BACKGROUND_EXECUTOR-thread-15): calling run()
04-20 20:13:31.056  20254-23455/com.example.machimanapc.howzit D/dalvikvm? threadid=12: exiting
04-20 20:13:31.056  20254-23455/com.example.machimanapc.howzit D/dalvikvm? threadid=12: bye!
04-20 20:13:31.060  20254-23457/com.example.machimanapc.howzit D/dalvikvm? threadid=14: exiting
04-20 20:13:31.060  20254-23457/com.example.machimanapc.howzit D/dalvikvm? threadid=14: bye!
04-20 20:13:31.061  20254-23458/com.example.machimanapc.howzit D/dalvikvm? threadid=15: exiting
04-20 20:13:31.061  20254-23458/com.example.machimanapc.howzit D/dalvikvm? threadid=15: bye!
04-20 20:13:31.064  20254-23460/com.example.machimanapc.howzit D/dalvikvm? threadid=20: exiting
04-20 20:13:31.064  20254-23460/com.example.machimanapc.howzit D/dalvikvm? threadid=20: bye!

`

1 个答案:

答案 0 :(得分:0)

我想出了 ListView listView = (ListView) findViewById(R.id.comment_list_view)我应该使用的 ListView listView = (ListView) findViewById(R.id.comment_list);感谢错误。