java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'...(...)'

时间:2019-01-15 07:32:48

标签: java android-studio nullpointerexception

我正在尝试链接我分别编写的两个代码(一个用于蓝牙连接初始化,另一个用于绘制图形),并且我收到了一个空指针异常,无法纠正。我已经在stackoverflow上检查了所有与NullPointerExcception相关的先前问题,似乎没有任何方法可以解决我的问题。任何帮助,我们将不胜感激。

因此,正如我所提到的,我试图链接分别编写的两个代码(一个用于蓝牙连接初始化,另一个用于使用MPAndroid图表库绘制带有随机点的图形。我的想法是一旦链接,即可获取蓝牙数据绘制在图表上)。我要做的是在我的蓝牙连接活动上添加一个单独的按钮,以移动到带有图形的活动。我已经检查了按钮,在将代码插入第二个活动之前,它工作正常。但是,一旦我插入代码并将所需的库包含在第二个活动中,我就会得到一个空指针异常错误。运行该应用程序时,蓝牙连接部分起作用,并且在单击第二项活动的按钮后,该应用程序停止运行,并在日志猫上显示以下错误。

Process: com.example.user.bluetooth_communication, PID: 7222
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.bluetooth_communication/com.example.user.bluetooth_communication.graphing}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.constraint.ConstraintLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.constraint.ConstraintLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
    at com.example.user.bluetooth_communication.graphing.onCreate(graphing.java:37)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)     

这是我添加到蓝牙连接活动(第一个活动)中的内容,然后继续进行第二个活动。

public Button but1;
public void init(){
        but1 = (Button)findViewById(R.id.but1);
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent toy = new Intent(MainActivity.this,graphing.class);
                startActivity(toy);
            }
        });
    }

,并在第一个活动的onCreate方法内添加了init()方法。因此,活动之间的切换效果很好。

下面给出的是第二个活动中代码的一部分,指出该错误。

public class graphing extends AppCompatActivity {
    private ConstraintLayout mainLayout;
    private LineChart mChart;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent toy = getIntent();
        mainLayout=(ConstraintLayout)findViewById(R.id.mainLayout);


        //create line Chart
        mChart= new LineChart(this);
        //add to main layout
        //mainLayout.addView(mChart);
        mainLayout.addView(mChart, new    AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));

        //customize line chart this requires earlier version of the library
        mChart.setDescription("");
        mChart.setNoDataTextDescription("No Data at the moment");

        //enable value highlighting
        //Set this to true on your Chart to allow highlighting per dragging over the chart surface when it is fully zoomed out. Default: true
        mChart.setHighlightPerDragEnabled(true);

错误指向该行

mainLayout.addView(mChart, new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));

这是mainActivity的xml文件。对于这个问题,我添加了仅添加的按钮以及顶部

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.example.user.bluetooth_communication.MainActivity">

    <Button
        android:id="@+id/but1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/btnDiscoverable_on_off"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="154dp"
        android:layout_marginBottom="381dp"
        android:text="Second" />


</RelativeLayout>

这是第二个活动的xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainLayout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".graphing">


</android.support.constraint.ConstraintLayout>     

0 个答案:

没有答案