问候语 当我点击片段上的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:FATAL EXCEPTION: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。在null对象引用上查看.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) at java.lang.reflect.Method.invoke(Native Method) 在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) at java.lang.reflect.Method.invoke(Native Method) 在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>
答案 0 :(得分:1)
1 Replace your code with this:
public class CameraFragment extends Fragment implements View.OnClickListener{
public CameraFragment() {
// Required empty public constructor
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
TextView textview = (TextView) view.findViewById(R.id.frag_text);
textView.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v){
Intent intent = new Intent(getActivity(), Main2Activity.class);
startActivity(intent);
}
答案 1 :(得分:0)
如果有帮助,试试这个,
@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(this);
// Inflate the layout for this fragment
return view;
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.frag_text :
//your code...
Intent intent = new Intent(getActivity(), Main2Activity.class);
startActivity(intent);
break;
default:
break;
}
}
答案 2 :(得分:0)
检查以下代码:
public class CameraFragment extends Fragment implements View.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(this);
// Inflate the layout for this fragment
return view;
}
@Override
public void onClick(View v){
Intent intent = new Intent(getActivity(), Main2Activity.class);
startActivity(intent);
}
}
答案 3 :(得分:0)
您可以在xml中设置点击处理程序(对于您的Textview):
android:onClick="onClick"
android:clickable="true"
在您的活动中:
public class yourActivity extends Activity {
public void onClick(View v) {
...
}
}
答案 4 :(得分:0)
最后,我已经解决了问题
最后一个错误是Nullpointer&amp;无法开展活动
我转到main2activity.java并执行以下操作:
1-删除
private TextView textview;
private View view;
更改以下内容
textView = (TextView) view.findViewById(R.id.textView2);
到
TextView textView = (TextView) findViewById(R.id.textView2);
我的应用现在工作正常。
感谢所有借我的时间来帮助我的人。 在另一个问题中见到你。