(java.lang.RuntimeException):无法启动活动ComponentInfo:java.lang.NullPointerException:println需要一条消息

时间:2014-12-29 13:15:15

标签: android

我是android新手。我正在努力解决上述错误。请帮我修复错误。我在名为" DetailsUsers.java"的活动中发现此错误。我的主要活动是正常运作。我点击主屏幕上的一个按钮,点击按钮后应用程序应该进入详细用户屏幕,而不是崩溃。请提供解决方案。

我的DetailsUsers.java代码:

import com.db.appform12.R;
import app.form.appform12.DatabaseHandler;
import app.form.appform12.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DetailsUsers  extends Activity implements OnClickListener{

    private String position = null;
    //User user = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details_users);

        Intent intent = getIntent();


        position = intent.getStringExtra("position");

        Log.d("value of position from intent", position);
        DatabaseHandler db;
        if (position !=null){
         db = new DatabaseHandler(this);
        }
        else {
            return;
        }




        User user = db.getUser(position);

        if (user !=null) {
            TextView textViewDetailName = (TextView) findViewById(R.id.textViewName);
            textViewDetailName.setText(user.getName());
        }
        else {
            return;
        }
//      TextView textViewDetailName = (TextView) findViewById(R.id.textViewName);
//      try {
//      textViewDetailName.setText(user.getName());
//      }catch (Exception e) {
//          System.out.print("Error is Here");
//      }

        TextView textViewDetailAddress = (TextView) findViewById(R.id.textViewAddress);
        try {
        textViewDetailAddress.setText(user.getAddress());
        }catch (Exception e) {
            System.out.print("Error is Here");
        }

        TextView textViewDetailsPhone = (TextView) findViewById(R.id.textViewPhone);
        try {
        textViewDetailsPhone.setText(user.getPhone());
        }catch (Exception e) {
            System.out.print("Error is Here");
        }

        TextView textViewDetailsEmail = (TextView) findViewById(R.id.textViewEmail);
        try {
        textViewDetailsEmail.setText(user.getEmail());
        }catch (Exception e) {
            System.out.print("Error is Here");
        }

        TextView textViewDetailsComments = (TextView) findViewById(R.id.textViewComments);
        try {
        textViewDetailsComments.setText(user.getComments());
        }catch (Exception e) {
            System.out.print("Error is Here");
        }
        Button backButton = (Button)findViewById(R.id.button2);
        backButton.setOnClickListener(this);

        Button editButton = (Button)findViewById(R.id.button1);
        editButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        if(v.getId() == R.id.button2) {
            Intent intent = new Intent(getApplicationContext(), ListUsers.class);
            startActivity(intent);
        }
        else if(v.getId() == R.id.button1) {
            Intent intent = new Intent(getApplicationContext(), EditUsers.class);
            intent.putExtra("position", position);
            startActivity(intent);
        }
    }
}

我的DetailsUsers.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="app.form.appform12.DetailsUsers" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:text="@string/full_name" />

    <TextView
        android:id="@+id/textViewPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewName"
        android:layout_marginTop="34dp"
        android:text="@string/phone_no" />

    <TextView
        android:id="@+id/textViewAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewPhone"
        android:layout_marginTop="62dp"
        android:text="@string/postal_address" />

    <TextView
        android:id="@+id/textViewEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewAddress"
        android:layout_centerVertical="true"
        android:text="@string/email_id" />

    <TextView
        android:id="@+id/textViewComments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewEmail"
        android:layout_marginTop="74dp"
        android:text="@string/comments_feedback" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:text="@string/back_btn" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewComments"
        android:layout_marginTop="41dp"
        android:layout_toRightOf="@+id/textViewAddress"
        android:text="@string/update_btn" />

</RelativeLayout>

我的完整ADBLOGCAT追踪:

12-29 08:04:06.579: I/dalvikvm(2798): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
12-29 08:04:06.579: W/dalvikvm(2798): VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
12-29 08:04:06.579: D/dalvikvm(2798): VFY: replacing opcode 0x6f at 0x0000
12-29 08:04:06.599: I/dalvikvm(2798): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
12-29 08:04:06.599: W/dalvikvm(2798): VFY: unable to resolve virtual method 11358: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
12-29 08:04:06.599: D/dalvikvm(2798): VFY: replacing opcode 0x6f at 0x0000
12-29 08:04:06.619: I/dalvikvm(2798): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
12-29 08:04:06.659: W/dalvikvm(2798): VFY: unable to resolve virtual method 9045: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
12-29 08:04:06.659: D/dalvikvm(2798): VFY: replacing opcode 0x6e at 0x000e
12-29 08:04:06.789: I/dalvikvm(2798): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-29 08:04:06.839: W/dalvikvm(2798): VFY: unable to resolve virtual method 370: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-29 08:04:06.839: D/dalvikvm(2798): VFY: replacing opcode 0x6e at 0x0002
12-29 08:04:06.859: I/dalvikvm(2798): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-29 08:04:06.879: W/dalvikvm(2798): VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getType (I)I
12-29 08:04:06.879: D/dalvikvm(2798): VFY: replacing opcode 0x6e at 0x0002
12-29 08:04:13.009: I/Choreographer(2798): Skipped 231 frames!  The application may be doing too much work on its main thread.
12-29 08:04:13.669: D/gralloc_goldfish(2798): Emulator without GPU emulation detected.
12-29 08:04:43.579: D/AndroidRuntime(2798): Shutting down VM
12-29 08:04:43.579: W/dalvikvm(2798): threadid=1: thread exiting with uncaught exception (group=0xb0f44648)
12-29 08:04:43.639: E/AndroidRuntime(2798): FATAL EXCEPTION: main
12-29 08:04:43.639: E/AndroidRuntime(2798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.db.appform12/app.form.appform12.DetailsUsers}: java.lang.NullPointerException: println needs a message
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.os.Looper.loop(Looper.java:137)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at java.lang.reflect.Method.invoke(Method.java:525)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at dalvik.system.NativeStart.main(Native Method)
12-29 08:04:43.639: E/AndroidRuntime(2798): Caused by: java.lang.NullPointerException: println needs a message
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.util.Log.println_native(Native Method)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.util.Log.d(Log.java:138)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at app.form.appform12.DetailsUsers.onCreate(DetailsUsers.java:31)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.app.Activity.performCreate(Activity.java:5133)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-29 08:04:43.639: E/AndroidRuntime(2798):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-29 08:04:43.639: E/AndroidRuntime(2798):     ... 11 more
12-29 08:04:43.739: D/dalvikvm(2798): GC_FOR_ALLOC freed 230K, 13% free 2739K/3148K, paused 58ms, total 73ms
12-29 08:04:49.950: I/Process(2798): Sending signal. PID: 2798 SIG: 9

我一直在努力解决这个问题。请提供建议并帮助我。提前谢谢。

2 个答案:

答案 0 :(得分:3)

positionnull。您无法将Log.d()null消息一起使用。如果您希望使用Log.d(),请先检查position并查看它是否为null,然后记录其他内容。如果您不希望position成为null,则需要了解您的活动是如何开始的,以及您的额外费用缺失的原因或null

答案 1 :(得分:0)

Log.d(“意图的位置值”,位置);检查位置值是否为null如果要使用System.out.println();它也会创建错误;而不是System.out.print();