繁体   English   中英

从Android上的Activity到ListActivity

[英]Going from Activity to ListActivity on Android

我是Android开发的新手,我需要帮助从Acticity转到Listactivity。 我的mainActivity中有一个按钮,这里是代码:

package com.mz47.kazeroon;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

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

        Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, DescriptionListActivity.class);
                //intent.putExtra("thetext", edt.getText().toString());
                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;
    }

}

如果我点击那个按钮,我的意图是:

package com.mz47.kazeroon;

import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.EventLogTags.Description;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DescriptionListActivity extends ListActivity {

    //Database Helper
    MyDatabaseHelper db = new MyDatabaseHelper(getApplicationContext());;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.desc_list_layout);

        setListAdapter(new DescriptionAdapter(this, 
                android.R.layout.simple_list_item_1,
                R.id.textView1, db.getAllDescriptionNames()));
        db.close();
    }

    private class DescriptionAdapter extends ArrayAdapter<String>{      

        public DescriptionAdapter(Context context, int resource,
                int textViewResourceId, String[] objects) {
            super(context, resource, textViewResourceId, objects);

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.list_items, parent, false);

            MyDescription[] descriptions = (MyDescription[])db.getAllDescriptions().toArray();


            ImageView imgv = (ImageView) row.findViewById(R.id.imageView1);
            TextView txtv = (TextView) row.findViewById(R.id.textView1);

            txtv.setText(descriptions[position].getDescriptionName());
            imgv.setImageBitmap(db.getImage(descriptions[position].getDescriptionID()).getImageData());

            return row;

        }

    }

}

我的问题是为什么我的应用程序意外停止了? 在清单中,我将DescriptionListActivity类注册为Activity这里是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mz47.kazeroon"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mz47.kazeroon.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity  android:name="com.mz47.kazeroon.DescriptionListActivity"></activity>
    </application>

</manifest>

logcat的:

03-11 16:10:26.887: D/dalvikvm(3186): GC_EXPLICIT freed 5K, 58% free 2569K/6023K, external 716K/1228K, paused 61ms
03-11 16:10:53.627: D/dalvikvm(3186): Debugger has detached; object registry had 1 entries
03-11 16:18:32.777: D/AndroidRuntime(16464): Shutting down VM
03-11 16:18:32.777: W/dalvikvm(16464): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 16:18:32.797: E/AndroidRuntime(16464): FATAL EXCEPTION: main
03-11 16:18:32.797: E/AndroidRuntime(16464): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mz47.kazeroon/com.mz47.kazeroon.DescriptionListActivity}: java.lang.NullPointerException
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.os.Looper.loop(Looper.java:123)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at java.lang.reflect.Method.invoke(Method.java:507)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at dalvik.system.NativeStart.main(Native Method)
03-11 16:18:32.797: E/AndroidRuntime(16464): Caused by: java.lang.NullPointerException
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at com.mz47.kazeroon.DescriptionListActivity.<init>(DescriptionListActivity.java:18)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at java.lang.Class.newInstanceImpl(Native Method)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at java.lang.Class.newInstance(Class.java:1409)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-11 16:18:32.797: E/AndroidRuntime(16464):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-11 16:18:32.797: E/AndroidRuntime(16464):    ... 11 more

其他xml文件:desc_list_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" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp" >

    </ListView>

</LinearLayout>

list_items.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="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxHeight="48dp"
        android:maxWidth="48dp"
        android:minHeight="48dp"
        android:minWidth="48dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

问题出在这里:

MyDatabaseHelper db = new MyDatabaseHelper(getApplicationContext());

方法getApplicationContext()仅在调用super.onCreate后返回上下文。 所以尝试改变你的活动:

MyDatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  db = new MyDatabaseHelper(getApplicationContext());
  //rest of your code
}

您已将MyDatabaseHelper数据库初始化为尚不存在的上下文

 MyDatabaseHelper db = new MyDatabaseHelper(getApplicationContext());;

它应该这样做:

MyDatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.db = new MyDatabaseHelper(getApplicationContext());
        setContentView(R.layout.desc_list_layout);

        setListAdapter(new DescriptionAdapter(this, 
                android.R.layout.simple_list_item_1,
                R.id.textView1, db.getAllDescriptionNames()));
        db.close();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM