我正在学习Android并遵循Wingnity的教程。我正在使用Android Studio 2.1.3。
我有2个java类(MainActivity和SimpleFragmentActivity)和3个布局(activity_main,activity_simple_fragment和片段简单(空))。
我不确定我的代码有什么问题。试图搜索解决方案,但代码似乎是正确的。
MainActivity
package com.example.ramosva.sunshine;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class SimpleFragmentActivity extends AppCompatActivity {
public SimpleFragmentActivity() {
// Required empty public constructor
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_fragment);
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
public static class PlaceholderFragment extends Fragment{
public PlaceholderFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.activity_simple_fragment, container, false);
Button btnClickMeGym = (Button)rootview.findViewById(R.id.btnSimpleFragGym);
btnClickMeGym.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "You clicked me Gym", Toast.LENGTH_SHORT).show();
}
});
return rootview;
}
}
}
SimpleFragmentActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Fragment"
android:id="@+id/btnSimpleFragmentMain"
android:layout_gravity="center_horizontal"
android:onClick="simples" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
布局
activity_main
<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"
tools:context="com.example.ramosva.sunshine.SimpleFragmentActivity">
<!-- TODO: Update blank fragment layout -->
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:text="@string/hello_blank_fragment" />-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:scaleType="centerCrop"
android:src="@drawable/carls" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:id="@+id/btnSimpleFragGym"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp" />
</RelativeLayout>
activity_simple_fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
fragment_simple
01-01 23:27:10.865 27075-27075/com.example.ramosva.sunshine W/System: ClassLoader referenced unknown path: /data/app/com.example.ramosva.sunshine-1/lib/arm
01-01 23:27:11.005 27075-27075/com.example.ramosva.sunshine W/System: ClassLoader referenced unknown path: /data/app/com.example.ramosva.sunshine-1/lib/arm
01-01 23:27:11.150 27075-27075/com.example.ramosva.sunshine W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-01 23:27:11.295 27075-27075/com.example.ramosva.sunshine D/SecWifiDisplayUtil: Metadata value : none
01-01 23:27:11.300 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{a7cb583 I.E...... R.....ID 0,0-0,0}
01-01 23:27:11.300 27075-27167/com.example.ramosva.sunshine D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-01 23:27:11.355 27075-27167/com.example.ramosva.sunshine D/libEGL: eglInitialize EGLDisplay = 0xae27f7c4
01-01 23:27:11.355 27075-27167/com.example.ramosva.sunshine I/OpenGLRenderer: Initialized EGL, version 1.4
01-01 23:27:11.360 27075-27167/com.example.ramosva.sunshine D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1
01-01 23:27:11.410 27075-27075/com.example.ramosva.sunshine W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
01-01 23:27:11.490 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
01-01 23:27:11.515 27075-27075/com.example.ramosva.sunshine I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ff58432 time:10330371
01-01 23:27:13.795 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-01 23:27:13.880 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-01 23:27:14.585 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-01 23:27:14.700 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-01 23:27:14.940 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-01 23:27:15.020 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-01 23:27:15.105 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-01 23:27:15.205 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-01 23:27:15.345 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-01 23:27:15.435 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-01 23:27:15.645 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-01 23:27:15.735 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-01 23:27:16.050 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-01 23:27:16.155 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-01 23:27:16.440 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-01 23:27:16.510 27075-27075/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-01 23:27:16.515 27075-27075/com.example.ramosva.sunshine D/AndroidRuntime: Shutting down VM
01-01 23:27:16.520 27075-27075/com.example.ramosva.sunshine E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ramosva.sunshine, PID: 27075
java.lang.IllegalStateException: Could not find method simples(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnSimpleFragmentMain'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10826)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
01-01 23:27:18.590 27075-27075/com.example.ramosva.sunshine I/Process: Sending signal. PID: 27075 SIG: 9
01-01 23:44:07.140 29325-29325/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-01 23:44:07.230 29325-29325/com.example.ramosva.sunshine D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-01 23:44:07.235 29325-29325/com.example.ramosva.sunshine D/AndroidRuntime: Shutting down VM
01-01 23:44:07.235 29325-29325/com.example.ramosva.sunshine E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ramosva.sunshine, PID: 29325
java.lang.IllegalStateException: Could not find method simples(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnSimpleFragmentMain'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10826)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
任何人都可以在这里指出错误。我只是按照Wingnity教程1:创建片段中使用的代码。
错误记录
{{1}}
答案 0 :(得分:0)
更改此行
setContentView(R.layout.activity_simple_fragment);
到
setContentView(R.layout.fragment_simple);
你不能在片段和root中覆盖2个相同的布局。它应该是不同的
答案 1 :(得分:0)
您尝试通过调用getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
中的SimpleFragmentActivity
向布局添加片段,但container
中的布局ID不是activity_simple_fragment.xml
。因此,您需要添加一个布局,并将其ID设置为container
中的activity_simple_fragment.xml
。
答案 2 :(得分:0)
它说java.lang.IllegalStateException: Could not find method simples(View) in a parent or ancestor Context for android:onClick attribute defined on view class
避免在XML中使用onClick。
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btnSimpleFragmentMain);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), SimpleFragmentActivity.class););
}
});
}
}
下面
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Fragment"
android:id="@+id/btnSimpleFragmentMain"
android:layout_gravity="center_horizontal"/>
//onClick removed