本文介绍了在运行时选项卡应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新android开发,我一直在创建一个包含两个标签的应用程序。在一个选项卡含有图像的网格,将显示,而在另一另一个页面将被显示。我创建了3 XML布局文件和3 Java类文件的code如下。 (我会写的$ C $下pageActivity.java以后)

MainActivity.java

 公共类MainActivity扩展TabActivity {

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    TabHost tabhost = getTabHost();

    则tabspec imgspec = tabhost.newTabSpec(图像);
    imgspec.setIndicator(图像);
    意图imgIntent =新的意图(这一点,ImageAdapter.class);
    imgspec.setContent(imgIntent);

    则tabspec pagespec = tabhost.newTabSpec(新页);
    pagespec.setIndicator(新页);
    意图pageIntent =新的意图(这一点,pageActivity.class);
    pagespec.setContent(pageIntent);

    tabhost.addTab(imgspec);
    tabhost.addTab(pagespec);
}

}
 

ImageAdapter.java

 公共类ImageAdapter扩展了BaseAdapter {
语境mContext;

公共整数[] mThumbIds = {R.drawable.ic_action_search,R.drawable.ic_launcher,R.drawable.img1};

公共ImageAdapter(上下文C){
    mContext = C;
}

公众诠释getCount将(){
    返回mThumbIds.length;
}

公共对象的getItem(INT位置){

    返回mThumbIds [位置]
}

公共查看getView(INT位置,查看convertView,ViewGroup中父){
     ImageView的ImageView的=新ImageView的(mContext);
        imageView.setImageResource(mThumbIds [位置]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(新GridView.LayoutParams(70,70));
        返回ImageView的;


}

@覆盖
众长getItemId(INT为arg0){
    // TODO自动生成方法存根
    返回0;
}

}
 

pageActivity.java

 公共类pageActivity延伸活动{

}
 

activity_main.xml

 < XML版本=1.0编码=UTF-8&GT?;
< TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@机器人:ID / tabhost
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>

<的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    < TabWidget
        机器人:ID =@机器人:ID /标签
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>

    <的FrameLayout
        机器人:ID =@机器人:ID / tabcontent
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =FILL_PARENT/>

    < / LinearLayout中>

< / TabHost>
 

img_layout.xml

 < XML版本=1.0编码=UTF-8&GT?;
< GridView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent>


< / GridView的>
 

page_layout.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>


< / LinearLayout中>
 

在运行时,应用程序崩溃。细算LogCat中似乎问题出在ImageAdapter.java文件。请帮忙 。

显示的LogCat中的错误以下

  08-27 19:16:14.772:E / AndroidRuntime(390):致命异常:主要
08-27 19:16:14.772:E / AndroidRuntime(390):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.prac1 / com.example.prac1.MainActivity}:java.lang.RuntimeException的:无法实例活动ComponentInfo {com.example.prac1 / com.example.prac1.ImageAdapter}:java.lang.InstantiationException:com.example.prac1.ImageAdapter
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.os.Handler.dispatchMessage(Handler.java:99)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.os.Looper.loop(Looper.java:130)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.ActivityThread.main(ActivityThread.java:3683)
08-27 19:16:14.772:E / AndroidRuntime(390):在java.lang.reflect.Method.invokeNative(本机方法)
08-27 19:16:14.772:E / AndroidRuntime(390):在java.lang.reflect.Method.invoke(Method.java:507)
08-27 19:16:14.772:E / AndroidRuntime(390):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
08-27 19:16:14.772:E / AndroidRuntime(390):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-27 19:16:14.772:E / AndroidRuntime(390):在dalvik.system.NativeStart.main(本机方法)
08-27 19:16:14.772:E / AndroidRuntime(390):java.lang.RuntimeException的:产生的原因无法实例活动ComponentInfo {com.example.prac1 / com.example.prac1.ImageAdapter}:java.lang中。 InstantiationException:com.example.prac1.ImageAdapter
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:654)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.widget.TabHost.setCurrentTab(TabHost.java:326)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.widget.TabHost.addTab(TabHost.java:216)
08-27 19:16:14.772:E / AndroidRuntime(390):在com.example.prac1.MainActivity.onCreate(MainActivity.java:32)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-27 19:16:14.772:E / AndroidRuntime(390):11 ...更多
08-27 19:16:14.772:E / AndroidRuntime(390):java.lang.InstantiationException:产生的原因com.example.prac1.ImageAdapter
08-27 19:16:14.772:E / AndroidRuntime(390):在java.lang.Class.newInstanceImpl(本机方法)
08-27 19:16:14.772:E / AndroidRuntime(390):在java.lang.Class.newInstance(Class.java:1409)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-27 19:16:14.772:E / AndroidRuntime(390):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
08-27 19:16:14.772:E / AndroidRuntime(390):20 ...更多
 

解决方案

您的问题是, ImageAdapter 不是活动因此,Android有不知道如何进行实例化,就好像是的。

您需要创建一个活动持有你的 ImageAdapter 并在该活动设置适配器上的的GridView

首先,改变img_layout.xml这样:

 < XML版本=1.0编码=UTF-8&GT?;
< GridView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / img_grid
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
 

让我们把你的新的活动 ImageGridActivity 。在你的 ImageGridActivity.onCreate()你会做的:

 的setContentView(R.id.img_layout);
GridView的网格=(GridView控件)findViewById(R.id.img_grid);
grid.setAdapter(新ImageAdapter(本));
 

I am new to android development and I've been creating an application containing two tabs . in one tab a Grid containing images will be displayed and on the other another page will be displayed . I've created 3 XML layout files and 3 java class files whose code is given below . (I'll be writing the code for pageActivity.java later on )

MainActivity.java

public class MainActivity extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost tabhost = getTabHost();

    TabSpec imgspec = tabhost.newTabSpec("Images");
    imgspec.setIndicator("Images");
    Intent imgIntent = new Intent(this,ImageAdapter.class);
    imgspec.setContent(imgIntent);

    TabSpec pagespec = tabhost.newTabSpec("NewPage");
    pagespec.setIndicator("New Page");
    Intent pageIntent = new Intent(this,pageActivity.class);
    pagespec.setContent(pageIntent);

    tabhost.addTab(imgspec);
    tabhost.addTab(pagespec);
}

}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
Context mContext;

public Integer[] mThumbIds  = {R.drawable.ic_action_search , R.drawable.ic_launcher , R.drawable.img1 };

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount(){
    return mThumbIds.length ;
}

public Object getItem(int position){

    return mThumbIds[position];
}

public View getView(int position , View convertView , ViewGroup parent){
     ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;


}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

}

pageActivity.java

public class pageActivity extends Activity {

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>

    </LinearLayout>

</TabHost>

img_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >


</GridView>

page_layout.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" >


</LinearLayout>

At runtime , the application crashes . After looking at the LogCat it seems that the problem is with the ImageAdapter.java file . Please Help .

The LogCat error is shown below

08-27 19:16:14.772: E/AndroidRuntime(390): FATAL EXCEPTION: main
08-27 19:16:14.772: E/AndroidRuntime(390): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prac1/com.example.prac1.MainActivity}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.prac1/com.example.prac1.ImageAdapter}: java.lang.InstantiationException: com.example.prac1.ImageAdapter
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.os.Looper.loop(Looper.java:130)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-27 19:16:14.772: E/AndroidRuntime(390):  at java.lang.reflect.Method.invokeNative(Native Method)
08-27 19:16:14.772: E/AndroidRuntime(390):  at java.lang.reflect.Method.invoke(Method.java:507)
08-27 19:16:14.772: E/AndroidRuntime(390):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-27 19:16:14.772: E/AndroidRuntime(390):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-27 19:16:14.772: E/AndroidRuntime(390):  at dalvik.system.NativeStart.main(Native Method)
08-27 19:16:14.772: E/AndroidRuntime(390): Caused by: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.prac1/com.example.prac1.ImageAdapter}: java.lang.InstantiationException: com.example.prac1.ImageAdapter
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.widget.TabHost.setCurrentTab(TabHost.java:326)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.widget.TabHost.addTab(TabHost.java:216)
08-27 19:16:14.772: E/AndroidRuntime(390):  at com.example.prac1.MainActivity.onCreate(MainActivity.java:32)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-27 19:16:14.772: E/AndroidRuntime(390):  ... 11 more
08-27 19:16:14.772: E/AndroidRuntime(390): Caused by: java.lang.InstantiationException: com.example.prac1.ImageAdapter
08-27 19:16:14.772: E/AndroidRuntime(390):  at java.lang.Class.newInstanceImpl(Native Method)
08-27 19:16:14.772: E/AndroidRuntime(390):  at java.lang.Class.newInstance(Class.java:1409)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
08-27 19:16:14.772: E/AndroidRuntime(390):  ... 20 more
解决方案

Your problem is that ImageAdapter is not an Activity and therefore Android has no idea how to instantiate it as though it were one.

You'll need to create an Activity which holds your ImageAdapter and within that Activity set the adapter on its GridView.

First, change img_layout.xml to this:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/img_grid"
android:layout_width="match_parent"
android:layout_height="match_parent" >

Let's call your new Activity ImageGridActivity. Within your ImageGridActivity.onCreate() you'll do:

setContentView(R.id.img_layout);
GridView grid = (GridView)findViewById(R.id.img_grid);
grid.setAdapter(new ImageAdapter(this));

这篇关于在运行时选项卡应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:03