如何在ListView父实例中实例化AppCompatActivity?

时间:2015-12-15 05:19:48

标签: android listview appcompat-v7-r23

我是android新手。我使用了一个扩展ListActivity的类。

public class ToLines extends ListActivity implements SimpleGestureFilter.SimpleGestureListener{}

但是,在这个类中,我想从AppCompatActivity中调用一个方法。所以我尝试像这样实例化AppCompatActivity类:

AppCompatActivity compat = new AppCompatActivity();
            compat.setSupportActionBar(toolbar);
            compat.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

但是,代码显示错误。这是我的logcat:

12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime: FATAL EXCEPTION: main
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime: Process: com.example.fahrudin.sjbapps, PID: 14754
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fahrudin.sjbapps/com.example.fahrudin.sjbapps.ToLines}: java.lang.NullPointerException
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:166)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5584)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:  Caused by: java.lang.NullPointerException
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:69)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:145)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:29)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:135)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:117)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:456)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at com.example.fahrudin.sjbapps.ToLines.onCreate(ToLines.java:86)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5447)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:166) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5584) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
12-15 12:00:20.923 14754-14754/com.example.fahrudin.sjbapps E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

当我的类扩展ListView时,我该怎么做才能调用AppCompatActivity的方法?

2 个答案:

答案 0 :(得分:1)

我认为您应该使用AppCompatActivity扩展您的主类并使用Listfragment而不是ListActivity,您将实现您想要的。

答案 1 :(得分:1)

你要创建一个完全错误的Activity类的实例。你应该这样使用

AppCompatActivity appActivity = (AppCompatActivity)ToLines.this
appActivity.setSupportActionBar(toolbar);
appActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);