【发布时间】:2017-05-31 11:01:27
【问题描述】:
问候 当我单击片段上的 TextView 时,应用程序崩溃了。请我需要专业知识来帮助我解决它。我在互联网上阅读了很多,但还没有解决方案,我尝试了很多方法,但都无法解决我的问题
package com.example.mymahine.nav1;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.VelocityTrackerCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class CameraFragment extends Fragment implements onClickListener{
private TextView textView;
public CameraFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
TextView textview = (TextView) view.findViewById(R.id.frag_text);
textview.setOnClickListener(new onClickListener);
// Inflate the layout for this fragment
return view;
}
@Override
public void onClick(View v){
Intent intent = new Intent(getActivity(), Main2Activity.class);
startActivity(intent);
}
/*
public void onActivityCreated(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
TextView tv2 = (TextView) getActivity().findViewById(R.id.textView1);
tv2.setOnClickListener(new view2.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Main2Activity.class);
startActivity(intent);
}
}
);
}
*/
}
下面是logcat:
06-01 13:14:28.607 4559-4559/com.example.mymachine.nav1 D/AndroidRuntime:关闭 VM 06-01 13:14:28.608 4559-4559/com.example.mymachine.nav1 E/AndroidRuntime: 致命异常: main 进程:com.example.mymachine.nav1,PID:4559 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.mymachine.nav1/com.example.mymachine.nav1.Main2Activity}:java.lang.NullPointerException:尝试调用虚拟方法 'android.view.View android.空对象引用上的 view.View.findViewById(int)' 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.view.View android.view.View.findViewById(int)' 在 com.example.mymachine.nav1.Main2Activity.onCreate(Main2Activity.java:17) 在 android.app.Activity.performCreate(Activity.java:6679) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
fragment_camera.xml:
<LinearLayout 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"
tools:context="com.example.shakeralmoosa.nav1.CameraFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/frag_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="195dp"
android:text="GO TO NEXT ACTIVITY"
android:textAlignment="center"
android:textSize="30sp"
android:clickable="true"
android:onClick="onClick"
android:focusable="true"
android:focusableInTouchMode="true"/>
</LinearLayout>
【问题讨论】:
-
这可能是问题,改成这个textview.setOnClickListener(this);
-
将
textview.setOnClickListener(new onClickListener);更改为textview.setOnClickListener(this);
标签: android