我一直在寻找错误,但是我找不到什么错误让应用程序停止并在我打开它时崩溃,我已经找到了每个错误并试图纠正它但我仍然不能弄清楚,我希望你能帮帮我吗?
这是我的
logcat的
12-25 03:33:42.214: E/AndroidRuntime(1559): FATAL EXCEPTION: main
12-25 03:33:42.214: E/AndroidRuntime(1559): Process: imamalsajadsayings.android.com, PID: 1559
12-25 03:33:42.214: E/AndroidRuntime(1559): java.lang.RuntimeException: Unable to start activity ComponentInfo{imamalsajadsayings.android.com/imamalsajadsayings.android.com.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.LinearLayout
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.os.Handler.dispatchMessage(Handler.java:102)
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.os.Looper.loop(Looper.java:136)
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-25 03:33:42.214: E/AndroidRuntime(1559): at java.lang.reflect.Method.invokeNative(Native Method)
12-25 03:33:42.214: E/AndroidRuntime(1559): at java.lang.reflect.Method.invoke(Method.java:515)
12-25 03:33:42.214: E/AndroidRuntime(1559): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-25 03:33:42.214: E/AndroidRuntime(1559): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-25 03:33:42.214: E/AndroidRuntime(1559): at dalvik.system.NativeStart.main(Native Method)
12-25 03:33:42.214: E/AndroidRuntime(1559): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.LinearLayout
12-25 03:33:42.214: E/AndroidRuntime(1559): at imamalsajadsayings.android.com.MainActivity.onCreate(MainActivity.java:20)
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.app.Activity.performCreate(Activity.java:5231)
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-25 03:33:42.214: E/AndroidRuntime(1559): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-25 03:33:42.214: E/AndroidRuntime(1559): ... 11 mor
MainActivity.java
package imamalsajadsayings.android.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById( R.id.state2 );
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("mytext","Second Activity");
startActivity(i);
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items TO the action bar IF it IS present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
SecondActivity.java
package imamalsajadsayings.android.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent i=getIntent();
String text=i.getStringExtra("mytext");
mTextView=(TextView)findViewById(R.id.two);
mTextView.setText(text);
}
}
second.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi"
android:textSize="50sp" />
</LinearLayout>
activity_main.xml中
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/linearlayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical"
android:background="@drawable/images" >
<com.google.ads.AdView
android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="a152b5665edfefd"
ads:loadAdOnCreate="true"
ads:adSize="BANNER" />
<TextView
android:id="@+id/state1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="قال (عليه السلام): (من كرمت عليه نفسه هانت عليه الدنيا) "
android:paddingTop="10sp"
android:clickable = "true"
style="?android:listSeparatorTextViewStyle" />
<TextView
android:id="@+id/state2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="قال (عليه السلام): (الخير كله في صيانة الإنسان نفسه) "
android:paddingTop="10sp"
style="?android:listSeparatorTextViewStyle"
android:clickable="true" />
</LinearLayout>
</ScrollView>
Mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="imamalsajadsayings.android.com"
android:versionCode="3"
android:versionName="1.2">
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/ic_launcher"
android:allowBackup="true"
android:theme="@style/AppTheme"
android:label="@string/app_name">
<activity android:name="imamalsajadsayings.android.com.MainActivity"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"/>
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
答案 0 :(得分:0)
它在你的堆栈跟踪中说:
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.LinearLayout
答案 1 :(得分:0)
根据您的logcat,此行可能是问题。
mTextView = (TextView) findViewById( R.id.state2 );
请检查资源R.id.state2
是否为TextView而不是线性布局!
答案 2 :(得分:0)
尝试清理您的项目。在eclipse下:Project -> Clean.
在Android Studio下:Build -> rebuild project.