2013-01-02 46 views
0

我正在使用配置活動來切換xml文件中使用RemoteViews的不同視圖的可見性。起初我還以爲,由於配置活動被稱爲是集件組之前,我wouldnt需要更新,所以我用:AppWidget不起作用

package com.example.test.clock.one; 

import android.app.Activity; 
import android.appwidget.AppWidgetManager; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.RemoteViews; 

public class chooseract extends Activity 
{ 
int mAppWidgetId; 
RemoteViews rv; 
AppWidgetManager appWidgetManager; 
Context context; 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  
} 
public void buttonhandler(View view) 
{ 
    rv = new RemoteViews(context.getPackageName(), R.layout.activity_main); 
    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    if (extras != null) 
    { 
     mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID); 
    } 
    switch(view.getId()) 
    { 
     case R.id.button1: 
     { 
      rv.setViewVisibility(R.id.analogClock1, View.VISIBLE); 
      rv.setViewVisibility(R.id.analogClock2, View.INVISIBLE); 

       finish(); 
          break; 
     } 
     case R.id.button2: 
     {    
      rv.setViewVisibility(R.id.analogClock1, View.INVISIBLE); 
      rv.setViewVisibility(R.id.analogClock2, View.VISIBLE); 
      finish(); 
          break; 

     } 
    } 


} 
} 

但只是設置一個空白的小部件(默認可見性是不可見的) 然後我嘗試更新appwidget切換使用

package com.example.test.clock.one; 

import android.app.Activity; 
import android.appwidget.AppWidgetManager; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.RemoteViews; 

public class chooseract extends Activity 
{ 
int mAppWidgetId; 
RemoteViews rv; 
AppWidgetManager appWidgetManager; 
Context context; 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  
} 
public void buttonhandler(View view) 
{ 
    rv = new RemoteViews(context.getPackageName(), R.layout.activity_main); 
    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    if (extras != null) 
    { 
     mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID); 
    } 
    switch(view.getId()) 
    { 
     case R.id.button1: 
     { 
      rv.setViewVisibility(R.id.analogClock1, View.VISIBLE); 
      rv.setViewVisibility(R.id.analogClock2, View.INVISIBLE); 
      break; 

     } 
     case R.id.button2: 
     {    
      rv.setViewVisibility(R.id.analogClock1, View.INVISIBLE); 
      rv.setViewVisibility(R.id.analogClock2, View.VISIBLE); 
      break; 

     } 
    } 

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
    appWidgetManager.updateAppWidget(mAppWidgetId, rv); 
    Intent resultValue = new Intent(); 
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 
    setResult(RESULT_OK, resultValue); 
    finish(); 
} 
} 

知名度後,但是當我觸摸按鈕與一個NullPointerException 任何想法這只是強制關閉?

回答

0
public class MainActivity extends Activity implements OnClickListener{ 

private int mAppWidgetId; 
private RemoteViews rv; 
private AppWidgetManager appWidgetManager; 

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



    rv = new RemoteViews(this.getPackageName(), R.layout.activity_main); 
    Button button1 = (Button)findViewById(R.id.button1); 
    Button button2 = (Button)findViewById(R.id.button2); 

    AnalogClock cos1 = (AnalogClock)findViewById(R.id.analogClock1); 
    AnalogClock cos2 = (AnalogClock)findViewById(R.id.analogClock2); 

    button1.setOnClickListener(this); 
    button2.setOnClickListener(this); 
} 

@Override 
public void onClick(View view) { 
    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    if (extras != null) 
    { 
     mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID); 
    } 
    switch(view.getId()) 
    { 
     case R.id.button1: 
     { 
      rv.setViewVisibility(R.id.analogClock1, View.VISIBLE); 
      rv.setViewVisibility(R.id.analogClock2, View.INVISIBLE); 
      break; 

     } 
     case R.id.button2: 
     {    
      rv.setViewVisibility(R.id.analogClock1, View.INVISIBLE); 
      rv.setViewVisibility(R.id.analogClock2, View.VISIBLE); 
      break; 

     } 
    } 

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 
    appWidgetManager.updateAppWidget(mAppWidgetId, rv); 
    Intent resultValue = new Intent(); 
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 
    setResult(RESULT_OK, resultValue); 
    finish(); 
} 

}

我添加一些修正,並不會引發錯誤。 :)

+0

:我欠你大時間:O – QuantumFoam