我正在尝试启动一个新片段,但我的应用程序崩溃说没有找到id的视图。我的目标是在单击按钮时启动此片段
我知道这已经被问了很多,但我找到的任何东西都无法解决我的问题。
这是输出:
05-22 15:45:11.125 3664-3664/com.example.tanner.sensortesting E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tanner.sensortesting, PID: 3664
java.lang.IllegalArgumentException: No view found for id 0x7f0c005f (com.example.tanner.sensortesting:id/sensorLayout) for fragment SensorActivity{2d46a59a #0 id=0x7f0c005f}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这是我的主要活动。当我取消注释第二个setContentView它可以工作,但它将两个sensorLayouts略微偏离彼此
package com.example.tanner.sensortesting;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button);
assert button != null;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//setContentView(R.layout.sensor_fragment);
getSupportFragmentManager().beginTransaction()
.add(R.id.sensorLayout, new SensorActivity())
.commit();
}
});
}
}
这是我要开始的片段:
package com.example.tanner.sensortesting;
import android.support.v4.app.Fragment;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by tanner on 5/15/16.
*/
public class SensorActivity extends Fragment implements SensorEventListener {
private SensorManager sensorManager;
private Sensor accelerometer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.sensor_fragment, container, false);
sensorManager = (SensorManager)getActivity().getSystemService(getActivity().SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
return rootView;
}
@Override
public void onSensorChanged(SensorEvent event) {
double x = event.values[0];
double y = event.values[1];
double z = event.values[2];
TextView xDegreeTextView = (TextView)getView().findViewById(R.id.xDegree);
TextView yDegreeTextView = (TextView)getView().findViewById(R.id.yDegree);
TextView zDegreeTextView = (TextView)getView().findViewById(R.id.zDegree);
xDegreeTextView.setText(x + "°");
yDegreeTextView.setText(y + "°");
zDegreeTextView.setText(z + "°");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
最后是SensorActivity的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: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.tanner.sensortesting.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sensorLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/xDegree"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/yDegree"
android:layout_below="@+id/xDegree"
android:layout_centerHorizontal="true"
android:layout_marginTop="106dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/zDegree"
android:layout_below="@+id/yDegree"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp" />
</RelativeLayout>
</RelativeLayout>
请帮忙!
答案 0 :(得分:0)
你应该将Fragment添加到activity_main.xml中的布局中,目前你的视图是id&#34; sensorLayout&#34;在片段本身的布局中。
尝试使用ID&#34; activity_main&#34;添加Framelayout改为使用activity_main.xml。
此外,最好将Fragment命名为SensorFragment。如果你扩展Fragment,它就是一个片段。如果你扩展Activity,那么它将是一个Activity。