我是初学者,并且是android的新手。我搜索但没有找到任何方法来解决我的问题在类似我的问题... 单击在main_activity中创建的按钮以显示detail_activity中的详细信息时,我有以下错误....可以帮助我吗? 我做错了什么?
这是完整的错误:
09-05 16:07:16.420: D/AndroidRuntime(1238): Shutting down VM
09-05 16:07:16.431: E/AndroidRuntime(1238): FATAL EXCEPTION: main
09-05 16:07:16.431: E/AndroidRuntime(1238): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workwithdata/com.example.workwithdata.DetailsActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class textView
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.app.ActivityThread.access$600(ActivityThread.java:156)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.os.Looper.loop(Looper.java:153)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.app.ActivityThread.main(ActivityThread.java:5299)
09-05 16:07:16.431: E/AndroidRuntime(1238): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 16:07:16.431: E/AndroidRuntime(1238): at java.lang.reflect.Method.invoke(Method.java:511)
09-05 16:07:16.431: E/AndroidRuntime(1238): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
09-05 16:07:16.431: E/AndroidRuntime(1238): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-05 16:07:16.431: E/AndroidRuntime(1238): at dalvik.system.NativeStart.main(Native Method)
09-05 16:07:16.431: E/AndroidRuntime(1238): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class textView
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-05 16:07:16.431: E/AndroidRuntime(1238): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:280)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.app.Activity.setContentView(Activity.java:1899)
09-05 16:07:16.431: E/AndroidRuntime(1238): at com.example.workwithdata.DetailsActivity.onCreate(DetailsActivity.java:16)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.app.Activity.performCreate(Activity.java:5182)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
09-05 16:07:16.431: E/AndroidRuntime(1238): ... 11 more
09-05 16:07:16.431: E/AndroidRuntime(1238): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.textView" on path: DexPathList[[zip file "/mnt/asec/com.example.workwithdata-2/pkg.apk"],nativeLibraryDirectories=[/mnt/asec/com.example.workwithdata-2/lib, /vendor/lib, /system/lib]]
09-05 16:07:16.431: E/AndroidRuntime(1238): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
09-05 16:07:16.431: E/AndroidRuntime(1238): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-05 16:07:16.431: E/AndroidRuntime(1238): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
09-05 16:07:16.431: E/AndroidRuntime(1238): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
09-05 16:07:16.431: E/AndroidRuntime(1238): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
09-05 16:07:16.431: E/AndroidRuntime(1238): ... 21 more
MainActivity.java是:
package com.example.workwithdata;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.example.workwithdata.R;
import com.example.workwithdata.data.Flower;
import com.example.workwithdata.data.FlowerData;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout= (LinearLayout) findViewById(R.id.layout);
List<Flower> Flowers = new FlowerData().GetFlowers();
for (final Flower flower : Flowers) {
Button button=new Button(this);
button.setText(flower.flowerName);
layout.addView(button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("flowerName", flower.flowerName);
intent.putExtra("imageResource", flower.imageResource);
intent.putExtra("instruction", flower.instructions);
startActivity(intent);
}
});
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和DetailsActivity.java:
package com.example.workwithdata;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class DetailsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
if (intent !=null) {
String flowerName = intent.getStringExtra("flowerName");
int imageResource = intent.getIntExtra("imageResource", 0);
String instruction = intent.getStringExtra("instrucion");
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(flowerName);
tv = (TextView) findViewById(R.id.textView2);
tv.setText(instruction);
ImageView IV = (ImageView) findViewById(R.id.image);
IV.setImageResource(imageResource);
}
}
}
和main_activity.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"
android:id="@+id/layout"
android:orientation="vertical">
</LinearLayout>
和details_activity.xml的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical" >
<textView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textAppearance="?android:attr/textAppearanceLarge" >
</textView>
<imageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1">
</imageView>
<textView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</textView>
</LinearLayout>
提前致谢
答案 0 :(得分:0)
谢谢大家......解决了我的问题...希望帮助别人。
一些要点......
1.确保安装了Android SDK的源代码,以便在您的布局中自动完成,您可以确定xml代码是真的。(专家开发人员的经验,不使用图形布局) (从SDK Manager下载并安装源代码)
2.将DetailActivity的布局代码更改为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textAppearance="?android:attr/textAppearance"/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />
</LinearLayout>